package com.hfh.work;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 * @Author hfh
 * @Date 2022 09 15 16 53
 **/
public class ClientDemo {
    public static void main(String[] args) throws IOException {
        ClientDemo cd = new ClientDemo();
        Socket s = new Socket(InetAddress.getLocalHost(),9999);
        cd.chat(s);
        s.close();

    }
    public void chat(Socket s) throws IOException {
        PrintWriter pw = new PrintWriter(s.getOutputStream());
        Scanner sc = new Scanner(System.in);
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        System.out.println("登录成功，开始聊天");
        while (true){
            String msg = sc.next();
            pw.println(msg);
            pw.flush();
            if("exit".equals(msg)){
                break;
            }

            String str = br.readLine();
            System.out.println(str);
        }

    }
}
